/**
 * com.future CO.,ltd.
 */

package com.future.springcloud.controller;

import com.future.springcloud.entities.Payment;
import com.future.springcloud.entities.util.CommonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;

/**
 * 类说明：
 */
@Controller
@RequestMapping("/comsumer/order")
public class OrderController {

    // 单机版
    //private static final String url = "http://localhost:8001";
    // 集群版
    private static final String url = "http://CLOUD-PAYMENT-SERVICE";


    @Autowired
    private RestTemplate restTemplate;

    @PostMapping("/create")
    @ResponseBody
    public CommonResult<Integer> create(@RequestBody Payment payment) {
        return restTemplate.postForObject(url + "/payment/create", payment, CommonResult.class);
    }

    @GetMapping(value = "/get/{id}")
    @ResponseBody
    public CommonResult<Payment> getPaymentById(@PathVariable("id") Long id) {
        return restTemplate.getForObject(url + "/payment/get/"+id, CommonResult.class);
    }

    @GetMapping(value = "/getForEntity/{id}")
    @ResponseBody
    public CommonResult<String> getPayment2(@PathVariable("id") Long id) {
        ResponseEntity<CommonResult> entity = restTemplate.getForEntity(url + "/payment/get/"+id, CommonResult.class);

        if (entity.getStatusCode().is2xxSuccessful()){
            return entity.getBody();
        }
        return new CommonResult<String>(444,"操作失败");
    }

}
